package GameObject;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class ImageObject {

	protected Point innerPoint;
	protected Point position;
	protected int width;
	protected int height;
	protected double rotation;
	protected float scaleX;
	protected float scaleY;
	protected BufferedImage bufferImg;

	//protected boolean isScalePosition = false;
	public ImageObject() {
		position = new Point();
		innerPoint = new Point();
		
	}

	public ImageObject(BufferedImage img) {
		init(img);
	}

	public void setPosition(int x, int y) {
		position.x = x;
		position.y = y;
	}
	private void init(BufferedImage img) {
		bufferImg = img;
		position = new Point(0, 0);
		scaleX = 1;
		scaleY = 1;
		rotation = 0;
		width = img.getWidth();
		height = img.getHeight();
		innerPoint = new Point(0, 0);
	}

	public Point getInnerPoint() {
		return innerPoint;
	}

	public void setInnerPoint(Point innerPoint) {
		this.innerPoint = innerPoint;
	}

	public Point getPosition() {
		return position;
	}

	public void setPosition(Point position) {
		this.position = position;
	}

	public double getRotation() {
		return rotation;
	}

	public void setRotation(double rotation) {
		this.rotation = rotation;
	}

	public float getScaleX() {
		return scaleX;
	}

	public void setScaleX(float scale) {
		this.scaleX = scale;
	}
	public float getScaleY() {
		return scaleY;
	}

	public void setScaleY(float scale) {
		this.scaleY = scale;
	}
	public BufferedImage getImage() {
		return bufferImg;
	}
	public void setImage(BufferedImage img) {
		bufferImg = img;
	}
	public int getX() {
		return (int) (position.x*scaleX);
	}

	public int getY() {
		return (int) (position.y*scaleY);
	}

	protected Point transform(Graphics2D g, int dx, int dy) {
		float x = (position.x + dx) * scaleX;
		float y = (position.y + dy) * scaleY;
		AffineTransform affineTransform = new AffineTransform();
		affineTransform.translate(x, y);
		affineTransform.rotate(rotation);
		affineTransform.translate(-x, -y);
		g.setTransform(affineTransform);


		x = (x - innerPoint.x * scaleX);
		y = (y - innerPoint.y * scaleY);
		return new Point((int) x, (int) y);
	}

	public void draw(Graphics2D g) {
		draw(g, 0, 0);
	}

	public void draw(Graphics2D g, int dx, int dy) {
		Point p = transform(g, dx, dy);
		int x = p.x + dx;
		int y = p.y + dy;

		g.drawImage(bufferImg,
				x, y, x + (int) (width * scaleX), y + (int) (height * scaleY),
				0, 0, width, height, null);
	}

	public void move(int dx, int dy) {
		position.x += dx;
		position.y += dy;
	}

	public boolean isCollision(Sprite sprite) {
		return false;
	}

	public Rectangle getBound() {
		float x = position.x * scaleX;
		float y = position.y * scaleY;

		x = (x - innerPoint.x * scaleX);
		y = (y - innerPoint.y * scaleY);
		return new Rectangle((int) x, (int) y, (int) (width * scaleX), (int) (height * scaleY));
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public static boolean isIn(Point p, Rectangle bound) {
		int x0 = bound.x;
		int x1 = bound.x + bound.width;
		int y0 = bound.y;
		int y1 = bound.y + bound.height;

		return (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1);
	}
}
